package com.sys.controller;

import com.sys.common.aop.Operation;
import com.sys.common.constant.Constant;
import com.sys.common.util.ResponseDateUtil;
import com.sys.common.util.ResultBuilder;
import com.sys.pojo.User;
import com.sys.service.UserService;
import com.sys.vo.UserRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZJF
 * @since 2021-04-01
 */
@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/listAllUser")
    @RequiresPermissions("user:list")
    @ApiOperation("获取全部用户")
    public ResponseDateUtil getAllUser(@RequestBody User user) {
        return ResultBuilder.success(userService.findListByPage(user));
    }

    @PostMapping("/addUser")
    @RequiresPermissions("user:add")
    @ApiOperation("添加新用户")
    @Operation("添加新用户")
    public ResponseDateUtil addUser(@RequestBody User user) {
        if (userService.add(user) > 0) {
            return ResultBuilder.success(Constant.ADDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.ADDFAILED);
        }
    }

    @PostMapping("/relationRole")
    @RequiresPermissions("user:relation")
    @ApiOperation("用户关联角色")
    @Operation("用户关联角色")
    public ResponseDateUtil relationRole(@RequestBody UserRoleVo vo) {
        if (userService.relationRole(vo)) {
            return ResultBuilder.success(Constant.ADDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.ADDFAILED);
        }
    }

    @PutMapping("/updUser")
    @RequiresPermissions("user:upd")
    @ApiOperation("更新用户信息")
    @Operation("更新用户信息")
    public ResponseDateUtil updUser(@RequestBody User user) {
        if (userService.updateData(user) > 0) {
            return ResultBuilder.success(Constant.UPDSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.UPDFAILED);
        }
    }

    @DeleteMapping("/delUser")
    @RequiresPermissions("user:del")
    @ApiOperation("删除用户信息")
    @Operation("删除用户信息")
    public ResponseDateUtil delUser(@RequestParam("id") Integer id) {
        if (userService.delete(id) > 0) {
            return ResultBuilder.success(Constant.DELSUCCESS);
        } else {
            return ResultBuilder.errorInfo(Constant.DELFAILED);
        }
    }

    @GetMapping(value = "/logout")
    @ApiOperation("注销")
    public ResponseDateUtil logout(@RequestParam("a") String a, @RequestParam("token") String token) {
        String url = "http://localhost:8080/logout";
        //post请求
        HttpMethod method = HttpMethod.POST;
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set(a, token);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
        //  执行HTTP请求
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return ResultBuilder.success(response.getBody());
    }
}
